import os
import feedparser

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

from google.appengine.ext import db

class FeedList(db.Model):				#Will be used for storing the feedurls
	blogurl = db.LinkProperty()			#and other housekeeping junk
	feedurl = db.LinkProperty()
	authname = db.StringProperty()
	authmail = db.EmailProperty()



class BlogEntryData(db.Model):			#Will be used for storing the entries. All entries from all blogs are treated equally.
	entryauth = db.StringProperty()		#While creating an entry, set blogauth from the dbase corresponding
	entryauthmail = db.EmailProperty()	#to the feedurl being fetched. This can be used as an author
						#identifier, as well as for customisation, say changing the email (which I do :) ).
	datetimepublished = db.DateTimeProperty()
	datetimeupdated = db.DateTimeProperty()
	entrylink = db.LinkProperty()
	entrytitle = db.StringProperty()
	entrycontent = db.TextProperty()
	entryid = db.StringProperty()



class MainPage(webapp.RequestHandler):
	def get(self):
		#fetch a list of entries
		entries = db.GqlQuery("SELECT * FROM BlogEntryData ORDER BY blogdatetime")

		#use a templating engine to create htmls :)
		path = os.path.join(os.path.dirname(__file__), '/htmls/index.html')
		self.response.out.write(template.render(path, template_values))

class RefreshDb(webapp.RequestHandler):
	def get(self):
		#refresh the db by checking for new feeds.
		#this url will be called regularly by a cronjob.

		#run a query to get a list of all feeds to fetch
		listoffeeds = db.GqlQuery("SELECT * FROM FeedList")

		for feed in listoffeed:
			fetchedfeed = urlfetch.fetch(feed.feedurl)
			parsedfeed = feedparser.parse(fetchedfeed.content)
			for newentry in parsedfeed.entries:
				#Run a query to dig out entries with same id
				oldentries = db.GqlQuery("SELECT * FROM BlogEntryData WHERE entryid = :1", newentry.id) 
				#An iterator is returned here, which may be empty
				#if it is not empty, then check whether the updated date has changed,
				#if changed delete the old entry and put the new one
				needtoupdate = 1
				for oldentry in oldentries:
					if oldentry.datetimeupdated != newentry.updated:
						oldentry.delete()
						needtoupdate = 0
						##SOME WORK TO BE DONE*****
						#Put the contents of the new entry into a new BlogEntryData instance
					else:
						needtoupdate = 1
						pass
				#after completion of iteration, or if the iterator was empty
				else:
					if needtoupdate == 1:
						#New entry was not entered so do it now
						##SOME WORK TO BE DONE*****
						#Put the contents of the new entry into a new BlogEntryData instance
					else:
						#dance
						pass






##SOME WORK TO BE DONE*****
###Feeling sleepy now, committing before going to sleep.
###the handler below has to be updated.
app = webapp.WSGIApplication([('/', MainPage)], debug=True)

def main():
	run_wsgi_app(app)

if __name__ == "__main__":
    main()

